// 一些基本功能：
// 1.获取时间  2.获取文件路径  3.获取文件大小  4.创建文件夹
#pragma once
#include <iostream>
#include <ctime>
#include <sys/stat.h>

namespace zzn
{
    namespace util
    {
        class data
        {
        public:
            static size_t GetTime()
            {
                return (size_t)time(nullptr);
            }
        };

        class file
        {
        public:
            static bool Exicts(const std::string &name)
            {
                struct stat st;
                if(stat(name.c_str(),&st)<0)
                {
                    return false;
                }

                return true;
            }

            static std::string GetPath(const std::string &name)
            {
                //./abc/def
                if (name.empty())
                {
                    return ".";
                }
                int pos = name.find_last_of("/\\");
                if (pos == std::string::npos)
                {
                    return ".";
                }
                return name.substr(0, pos + 1);
            }

            static void CreateDirectory(const std::string &pathname)
            {
                //./abc/def/sss
                if (pathname.empty())
                {
                    return;
                }
                if (Exicts(pathname))
                {
                    return;
                }
                size_t pos = 0, idx = 0;
                while (idx < pathname.size())
                {
                    size_t pos = pathname.find_first_of("/\\", idx);
                    if (pos == std::string::npos)
                    {
                        mkdir(pathname.c_str(), 0777);
                        return;
                    }
                    std::string subdir = pathname.substr(0, pos);
                    if (subdir == "." || subdir == "..")
                    {
                        idx = pos + 1;
                        continue;
                    }
                    if (Exicts(subdir))
                    {
                        idx = pos + 1;
                        continue;
                    }
                    mkdir(subdir.c_str(), 0777);
                    idx = pos + 1;
                }
            }
        };
    }
}